﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Product")]
    public class T_Product :IEntity<T_Product>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Column(TypeName="varchar")]
        [StringLength(64)]
        public string ProductCode { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ProductName { get; set; }
        public int ProductType { get; set; }
        public int ProductSubType { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(2048)]
        public string Images { get; set; }
        [Column(TypeName = "ntext")]
        public string Description { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string Version { get; set; }
        public DateTime CreateDate { get; set; }
        public int AreaCode { get; set; }
        /// <summary>
        /// 评分
        /// </summary>
        public int Value { get; set; }
        public int BusinessId { get; set; }
        /// <summary>
        /// 配餐信息
        /// </summary>
        [Column(TypeName = "ntext")]
        public string Catering { get; set; }
        [Column(TypeName = "int")]
        public int CapsPeople { get; set; }
        /// <summary>
        /// 原价
        /// </summary>
        public decimal OriginalPrice { get; set; }
         /// <summary>
         /// 销售价
         /// </summary>
        public decimal PurchasePrice { get; set; }

        public override T_Product Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Product Find(System.Linq.Expressions.Expression<Func<T_Product, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Product.FirstOrDefault(where);
                if (item != null)
                {
                    this.AreaCode = item.AreaCode;
                    this.BusinessId = item.BusinessId;
                    this.Catering = item.Catering;
                    this.CreateDate = item.CreateDate;
                    this.Description = item.Description;
                    this.Id = item.Id;
                    this.Images = item.Images;
                    this.ProductCode = item.ProductCode;
                    this.ProductName = item.ProductName;
                    this.ProductSubType = item.ProductSubType;
                    this.ProductType = item.ProductType;
                    this.Value = item.Value;
                    this.Version = item.Version;
                    this.CapsPeople = item.CapsPeople;
                    this.PurchasePrice = item.PurchasePrice;
                    this.OriginalPrice = item.OriginalPrice;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Product, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Product.Any(where);
            }
        }

        public override T_Product FindLast(System.Linq.Expressions.Expression<Func<T_Product, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Product.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.AreaCode = item.AreaCode;
                    this.BusinessId = item.BusinessId;
                    this.Catering = item.Catering;
                    this.CreateDate = item.CreateDate;
                    this.Description = item.Description;
                    this.Id = item.Id;
                    this.Images = item.Images;
                    this.ProductCode = item.ProductCode;
                    this.ProductName = item.ProductName;
                    this.ProductSubType = item.ProductSubType;
                    this.ProductType = item.ProductType;
                    this.Value = item.Value;
                    this.Version = item.Version;
                    this.CapsPeople = item.CapsPeople;
                    this.PurchasePrice = item.PurchasePrice;
                    this.OriginalPrice = item.OriginalPrice;
                    return this;
                }
                return null;
            }
        }
    }
}
